<?php
session_start();

include ("../utils/ConnectionIntra.class.php");
include ("../config/config.php");
include ("../model/users_model.php");

if (isset($_POST['login']) && isset($_POST['password']))
  {
    $password_crypted = SHA1($_POST['password'] . SALT);
    $res = user_exist($_POST['login'], $password_crypted);
    if ($res)
      {
	$_SESSION['isConnected'] = 'true';
	$_SESSION['login'] = $res['username'];
	$_SESSION['password'] = $res['password'];
	$_SESSION['right'] = $res['right'];
      }
    else
      {
	$connect = new ConnectionIntra($_POST['login'], $_POST['password']);
	if ($connect->checkIsConnected())
	  {
	    if ($connect->parseRapportStudent() == true)
	      {
		add_user_in_base($_POST['login'], $password_crypted, 1);
		$_SESSION['isConnected'] = 'true';
		$_SESSION['login'] = $_POST['login'];
		$_SESSION['password'] = $password_crypted;
		$_SESSION['right'] = 1;
	      }
	  }
	else
	  {
	    $_SESSION['isConnected'] = 'false';
	  }
      }
  }

header("Location: ../index.php");
exit;

?>